/**
 * @author David Young
 */
package csab;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
/**
 * This state represents the state where
 * the player is actual engaged in game play.
 * @author David Young
 */
public class PlayState extends BasicGameState {
	private int stateid;
	private LevelBase level;
	private boolean exit;
	private boolean reload;
	
	public PlayState(int id)
	{
		exit = false;
		reload = false;
		stateid = id;
	}
	/* (non-Javadoc)
	 * @see org.newdawn.slick.state.GameState#init(org.newdawn.slick.GameContainer, org.newdawn.slick.state.StateBasedGame)
	 */
	@Override
	public void init(GameContainer container, StateBasedGame game) throws SlickException 
	{
		reload = true;
	}
	
	private void load(GameContainer container, StateBasedGame game) throws SlickException
	{
		level = new LevelBase("resource/maps/TestMap1.tmx");
		level.init(container);
		reload = false;
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.state.GameState#render(org.newdawn.slick.GameContainer, org.newdawn.slick.state.StateBasedGame, org.newdawn.slick.Graphics)
	 */
	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException 
	{
		if(!reload)
			level.render(container, g);
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.state.GameState#update(org.newdawn.slick.GameContainer, org.newdawn.slick.state.StateBasedGame, int)
	 */
	@Override
	public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException 
	{ 
		if(exit)
		{
			exit = false;
			reload = true;
			level.cleanup();
			game.enterState(EntryPoint.MENUSTATE_ID);
		} 
		else if(reload)
		{
			load(container, game);
		}
		else
		{
			level.update(container, delta);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.newdawn.slick.state.GameState#keyPressed(int, char)
	 */
	@Override
	public void keyPressed(int key, char c)
	{
		if(key == Input.KEY_ESCAPE)
			exit = true;
	}


	/* (non-Javadoc)
	 * @see org.newdawn.slick.state.BasicGameState#getID()
	 */
	@Override
	public int getID() {
		return stateid;
	}
}
